package avicit.bdp.dcs.utils.api.request;

import avicit.bdp.dcs.utils.api.Configuration;
import avicit.bdp.dcs.utils.api.enums.Key;
import avicit.bdp.dcs.utils.api.enums.Method;
import avicit.bdp.dcs.utils.api.header.HeaderAdapter;
import avicit.bdp.dcs.utils.api.header.HeaderAdapterUtil;
import avicit.bdp.dcs.utils.api.response.ResponseAdapter;
import avicit.bdp.dcs.utils.api.response.ResponseAdapterUtil;
import avicit.platform6.core.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GetRequestAdapter extends RequestAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(GetRequestAdapter.class);

    private static GetRequestAdapter instance;
    private static RestTemplate restTemplate;

    private GetRequestAdapter() {
//        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//        requestFactory.setConnectTimeout(5000);
//        requestFactory.setReadTimeout(5000);
//        restTemplate = new RestTemplate(requestFactory);

        HttpsClientHttpRequestFactory requestFactory = new HttpsClientHttpRequestFactory();
        requestFactory.setConnectTimeout(20000);
        requestFactory.setReadTimeout(20000);
        restTemplate = new RestTemplate(requestFactory);
        LOG.info("HttpsClientHttpRequestFactory>>>");

        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        messageConverters.add(new FormHttpMessageConverter());

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.ALL));
        messageConverters.add(converter);

        restTemplate.getMessageConverters().addAll(messageConverters);
    }

    public synchronized static GetRequestAdapter instance() {
        if (instance == null) {
            instance = new GetRequestAdapter();
        }
        return instance;
    }

    /**
     * 调用API接口
     * @param readerSliceConfig
     * @param url
     * @return
     */
    public String callApi(Configuration readerSliceConfig, String url){
        try{
            HeaderAdapter headerAdapter = HeaderAdapterUtil.getHeaderAdapter(Method.GET.getName());
            HttpEntity<Object> httpEntity = (HttpEntity<Object>) headerAdapter.buildHttpEntity(readerSliceConfig);
            ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);

            // 解析返回结果数据
            ResponseAdapter responseAdapter = ResponseAdapterUtil.getResponseAdapter(readerSliceConfig.getString(Key.RESPONSE_TYPE));
            return responseAdapter.parseResponseData(readerSliceConfig, result);
        }catch (Exception e){
            e.printStackTrace();
            String msg = String.format("调用API接口失败,%s", e.getMessage());
            LOG.error(msg);
            throw new BusinessException(msg);
        }
    }

}
